package main

import (
	_ "image/jpeg"
	_ "image/png"

	"github.com/makiuchi-d/gozxing"
	"github.com/makiuchi-d/gozxing/oned"
	"image"
	"log"
	"os"
)

func main() {
	testGoZXingDecoder("images/20200722183909.png")
}

func testGoZXingDecoder(filename string) {

	//if filename == "" {
	//	filename = decodeTarget
	//}
	//
	//if !isPerformanceTest {
	//	log.Println("testGoZXingDecoder start")
	//}

	img, err := os.Open(filename)
	decode, _, err := image.Decode(img)
	if err != nil {
		panic(err)
		return
	}

	zxBitmap, err := gozxing.NewBinaryBitmapFromImage(decode)

	r := oned.NewCode128Reader() //zxingqr.NewQRCodeReader() //qrcodereader和qrdecoder的区别在于read会尝试多种格式（TRY_HARDER），而decoder不会
	result, err := r.Decode(zxBitmap, map[gozxing.DecodeHintType]interface{}{
		gozxing.DecodeHintType_CHARACTER_SET:    "UTF-8",
		gozxing.DecodeHintType_POSSIBLE_FORMATS: gozxing.BarcodeFormat_CODE_128, //gozxing.BarcodeFormat_QR_CODE,
		gozxing.DecodeHintType_TRY_HARDER:       true,
	})
	if err != nil {
		log.Println(err)
		return
	}

	log.Println("qrCode text:", result.String())

	//if !isPerformanceTest {
	//	log.Println("testGoZXingDecoder end")
	//}

}
